package com.hospital.hl7package.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.hl7package.module.mapper.OperationMasterMapper;
import com.hospital.hl7package.module.mapper.OperationNameMapper;
import com.hospital.hl7package.module.mapper.OperationScheduleMapper;
import com.hospital.hl7package.module.mapper.ScheduledOperationNameMapper;
import com.hospital.hl7package.module.entity.OperationMaster;
import com.hospital.hl7package.module.entity.OperationName;
import com.hospital.hl7package.module.entity.OperationSchedule;
import com.hospital.hl7package.module.service.OperationMasterService;
import com.hospital.hl7package.module.mapper.OperationMasterMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lvyao
 * @since 2020-09-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class OperationMasterServiceImpl extends ServiceImpl<OperationMasterMapper, OperationMaster> implements OperationMasterService {
    @Resource
    private OperationMasterMapper operationMasterMapper;

    @Resource
    private OperationNameMapper operationNameMapper;

    @Resource
    private OperationScheduleMapper operationScheduleMapper;

    @Resource
    private ScheduledOperationNameMapper scheduledOperationNameMapper;

    @Override
    public int getMaxOperId(String patientId, String visitId) {
        return operationMasterMapper.getMaxOperId(patientId, visitId);
    }

    @Override
    public Boolean saveOperation(Map<String, Object> map) {
        log.info(map + "手麻保存内容");
        OperationMaster operationMaster = (OperationMaster) map.get("OperationMaster");
        String oneKey = operationMaster.getPatientId() + operationMaster.getVisitId() + operationMaster.getOperId();
        OperationMaster operationMaster1 = null;
        operationMaster1 = getOperation(oneKey);
        List<OperationName> operationNameList = (List<OperationName>) map.get("OperationName");
        OperationSchedule operationSchedule = operationScheduleMapper.getOperationSchedule(oneKey);
        if (operationMaster1 == null && operationSchedule != null) {
            if (operationMasterMapper.insert(operationMaster) > 0) {
                log.info(operationMaster + "插入手术主表");
                if (operationNameList.isEmpty() || operationNameList == null || operationNameList.size() < 1) {
                    return false;
                }
                for (OperationName operationName : operationNameList) {
                    if (operationNameMapper.insert(operationName) <= 0) {
                        return false;
                    }
                    log.info(operationName + "插入手术名称主表");
                }
                log.info(operationScheduleMapper.getOperationSchedule(oneKey) + "删除手术申请表");
                if (operationScheduleMapper.deleteOperationSchedule(oneKey)) {
                    log.info(scheduledOperationNameMapper.getScheduledOperationName(oneKey) + "删除手术申请名称表");
                    if (!scheduledOperationNameMapper.deleteScheduledOperationName(oneKey)) {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            log.info(operationMaster1 + "更新前删除的手术主表");
            if (operationMaster1 != null && operationSchedule == null && operationMasterMapper.getUpdateFlag()==null?false:operationMasterMapper.getUpdateFlag()) {
                if (operationMasterMapper.deleteOperation(oneKey)) {
                    log.info(operationMaster + "更新手术主表");
                    if (operationMasterMapper.insert(operationMaster) <= 0) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public OperationMaster getOperation(String oneKey) {
        return operationMasterMapper.getOperation(oneKey);
    }

    @Override
    public Boolean deleteOperation(String oneKey) {
        return operationMasterMapper.deleteOperation(oneKey);
    }

    @Override
    public Boolean getUpdateFlag() {
        return operationMasterMapper.getUpdateFlag();
    }

}
